package com.lovo.invoicing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.invoicing.bean.LogBean;
import com.lovo.invoicing.bean.PageBean;
import com.lovo.invoicing.mapper.LogMapper;
import com.lovo.invoicing.service.ILogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, LogBean> implements ILogService {

    @Override
    public IPage<LogBean> queryLogsByPage(PageBean pageBean) {
        QueryWrapper<LogBean> qw = new QueryWrapper<>();
        if (pageBean.getStartDate()!=null && !"".equals(pageBean.getStartDate())){
            qw.ge("log_date",pageBean.getStartDate());
        }
        if (pageBean.getEndDate()!=null && !"".equals(pageBean.getEndDate())){
            qw.le("log_date",pageBean.getEndDate());
        }
        return baseMapper.selectPage(new Page<>(pageBean.getPageNo(), pageBean.getPageSize()),qw);
    }

    @Override
    @Transactional
    public void addLog(String userName,String action) {
        LogBean logBean = new LogBean();
        logBean.setLogDate(LocalDate.now());
        DateTimeFormatter df = DateTimeFormatter.ofPattern("HH:mm");
        String logTime = LocalTime.now().format(df);
        String content ="用户"+userName+"在"+logTime+action;
        logBean.setLogContent(content);
        baseMapper.insert(logBean);
    }
}
